<template>
	<view class="container">
		<view class="tui-searchbox">
			<view class="tui-search-input">
				<!-- #ifdef APP-PLUS || MP -->
				<icon type="search" :size='13' color='#333'></icon>
				<!-- #endif -->
				<!-- #ifdef H5 -->
				<view>
					<tui-icon name="search" :size='16' color='#333'></tui-icon>
				</view>
				<!-- #endif -->
				<input confirm-type="search" @confirm="gocate" :placeholder="hot[0].name" :focus="true" auto-focus
				 placeholder-class="tui-input-plholder" class="tui-input" v-model.trim="key" />
				<!-- #ifdef APP-PLUS || MP -->
				<icon type="clear" :size='13' color='#bcbcbc' @tap="cleanKey" v-show="key"></icon>
				<!-- #endif -->
				<!-- #ifdef H5 -->
				<view @tap="cleanKey" v-show="key">
					<tui-icon name="close-fill" :size='16' color='#bcbcbc'></tui-icon>
				</view>
				<!-- #endif -->
			</view>
			<view class="tui-cancle" @tap="back">取消</view>
		</view>

		<view class="tui-search-history" v-if="history.length>0">
			<view class="tui-history-header">
				<view class="tui-search-title">搜索历史</view>
				<tui-icon name="delete" :size='14' color='#333' @tap="openActionSheet" class="tui-icon-delete"></tui-icon>
			</view>
			<view class="tui-history-content">
				<block v-for="(item,index) in history" :key="index">
					<tui-tag type="gray" shape="circle" @click="goSeachGoodslist(item)">{{item}}</tui-tag>
				</block>
			</view>
		</view>

		<view class="tui-search-hot">
			<view class="tui-hot-header">
				<view class="tui-search-title">大家正在搜</view>
			</view>
			<view class="tui-hot-content">
				<block v-for="(item,index) in hot" :key="index">
					<tui-tag type="gray" shape="circle" @click="goSeachGoodslist(item.name)">{{item.name}}</tui-tag>
				</block>
			</view>
		</view>
		<tui-actionsheet :show="showActionSheet" :tips="tips" @click="itemClick" @cancel="closeActionSheet"></tui-actionsheet>
	</view>

</template>

<script>
	import tuiIcon from "../../../appComponent/uni/icon/icon.vue"
	import tuiTag from "../component/tag/tag.vue"
	import tuiActionsheet from "../component/actionsheet/actionsheet.vue"
	var common = require('../../../utils/common.js');
	var app = getApp();
	var _this;
	const getSearchlist = () => {
		common.post('Appoint_new&a=search_hot', {}, data => {
			console.log("nnnnnnn", data)
			_this.hot = data.search_hot_list;
		});
	};
	export default {
		components: {
			tuiIcon,
			tuiTag,
			tuiActionsheet
		},
		data() {
			return {
				history: [

				],
				hot: [],
				key: "",
				showActionSheet: false,
				tips: "确认清空搜索历史吗？",
				searchHot: '',
			}
		},
		onShow() {
			try {
				var vv = uni.getStorageSync('searchLocal') || "";
				if (vv) {
					_this.history = JSON.parse(vv);
				} else {
					_this.history = [];
				}
			} catch (e) {
				console.log(e)
			}
		},
		onLoad() {
			_this = this;
			getSearchlist();
			// this.loadData();
		},
		methods: {
			gocate() {
				console.log("输入框的字", _this.key)
				var newArr = _this.history;
				console.log(newArr.length)
				if(_this.key == ''){
					uni.showModal({
						title: common.changeTxt('提示'),
						content: common.changeTxt('请输入内容搜索'),
						showCancel: false,
						confirmText: common.changeTxt('好的'),
						success: function(res) {
							if (res.confirm) {
								return false
							}
						}
					});
					return false
				}
				if (_this.history.indexOf(_this.key) == -1) {
					newArr.unshift(_this.key);
				}
				if (newArr.length >= 11) {
					newArr.pop()
				}
				console.log(newArr.length)
				newArr = JSON.stringify(newArr); //转换成json格式的数据
				uni.setStorage({
					key: 'searchLocal',
					data: newArr
				});
				// _this.key = "";
				uni.navigateTo({
					url: '/pages/appoint/goods/searchCate?name=' + _this.key
				})
			},
			goSeachGoodslist(name) {
				console.log("nnnnnnnnn", name)
				uni.navigateTo({
					url: '/pages/appoint/goods/searchCate?name=' + name
				})
			},
			back: function() {
				uni.navigateBack();
			},
			cleanKey: function() {
				_this.key = ''
			},
			closeActionSheet: function() {
				_this.showActionSheet = false
			},
			openActionSheet: function() {
				_this.showActionSheet = true
			},
			itemClick: function(e) {
				let index = e.index;
				if (index == 0) {
					_this.showActionSheet = false;
					_this.history = []
					uni.setStorage({
						key: 'searchLocal',
						data: _this.searchKey
					});
				}
			}
		}
	}
</script>

<style>
	page {
		color: #333;
		background: #fff;
	}

	.container {
		padding: 0 30rpx 30rpx 30rpx;
		box-sizing: border-box;
	}

	.tui-searchbox {
		padding: 30rpx 0;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-search-input {
		width: 100%;
		height: 66rpx;
		border-radius: 35rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		background: #f2f2f2;
		display: flex;
		align-items: center;
		flex-wrap: nowrap;
	}

	.tui-input {
		flex: 1;
		color: #333;
		padding: 0 16rpx;
		font-size: 28rpx;
	}

	.tui-input-plholder {
		font-size: 28rpx;
		color: #b2b2b2;
	}

	.tui-cancle {
		color: #888;
		font-size: 28rpx;
		padding-left: 30rpx;
		flex-shrink: 0;
	}

	.tui-history-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 0;
	}

	.tui-icon-delete {
		padding: 10rpx;
	}

	.tui-search-title {
		color: #999999;
		font-size: 24rpx;
	}

	.tui-hot-header {
		padding: 30rpx 0;
	}

	.tui-tag-class {
		display: inline-block;
		margin-bottom: 20rpx;
		margin-right: 20rpx;
		font-size: 26rpx !important;
	}

	/deep/ .tui-tag-fillet {
		border-radius: 0;
	}
</style>
